package com.vitabuybuy.tv.ui.cart.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.AddressListDto
import com.vitabuybuy.tv.dto.CouponCenterDto
import com.vitabuybuy.tv.dto.SettlementDto
import com.vitabuybuy.tv.network.AddressRepository
import com.vitabuybuy.tv.network.BiddingRepository
import com.vitabuybuy.tv.network.CartRepository
import kotlin.collections.ArrayList
import kotlin.collections.HashMap

class ConfirmOrderViewModel : BaseViewModel() {

    val settlementDto = MutableLiveData<SettlementDto>()
    val addressList = MutableLiveData<MutableList<AddressListDto>>()
    val canUserCoupnList = MutableLiveData<MutableList<CouponCenterDto>>()
    val chooseStoreCouponIds = HashMap<String, Int>()

    fun calculateCart(idList: ArrayList<Int>?, from: Int?) {
        rxHttpRequest {
            onRequest = {
                settlementDto.value = CartRepository.calculateCart(idList, from).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun foundCanUserCoupons(params: Map<String, Any?>) {
        rxHttpRequest {
            onRequest = {
                canUserCoupnList.value = CartRepository.foundCanUserCoupons(params).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun getAddressList() {
        rxHttpRequest {
            onRequest = {
                addressList.value = AddressRepository.getAddressList().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun submitOrder(
        idList: ArrayList<Int>?,
        couponIdList: ArrayList<Int>?,
        id: Int?,
        email: String,
        remark: HashMap<Int, String>,
        liveId: Int?
    ) {
        rxHttpRequest {
            onRequest = {
                val dto = if (liveId == 0)
                    CartRepository.submitOrder(idList, couponIdList, id, email,remark).await()
                else
                    CartRepository.submitOrder(idList, couponIdList, id, email,remark,liveId).await()
                defUI.msgEvent.postValue(MessageModel(1, obj = dto.id))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun submitGroupBuyCreate(groupBuyId: Int?, itemId: Int, addressId: Int?, email: String, remark: HashMap<Int, String>) {
        rxHttpRequest {
            onRequest = {
                val dto = CartRepository.submitGroupBuyCreate(itemId, groupBuyId, addressId, email,remark)
                    .await()
                defUI.msgEvent.postValue(MessageModel(1, obj = dto.id))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun submitGroupBuyJoin(groupBuyId: Int?, itemId: Int, addressId: Int?, email: String, remark: HashMap<Int, String>) {
        rxHttpRequest {
            onRequest = {
                val dto =
                    CartRepository.submitGroupBuyJoin(itemId, groupBuyId, addressId, email,remark).await()
                defUI.msgEvent.postValue(MessageModel(1, obj = dto.id))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun submitGroupBuyBylimitJoin(groupBuyId: Int?, itemId: Int, addressId: Int?, email: String, remark: HashMap<Int, String>) {
        rxHttpRequest {
            onRequest = {
                val dto =
                    CartRepository.submitGroupBuyBylimitJoin(itemId, groupBuyId, addressId, email,remark)
                        .await()
                defUI.msgEvent.postValue(MessageModel(1, obj = dto.id))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun payOrder(id: Int?, paymentMethod: String) {
        rxHttpRequest {
            onRequest = {
                CartRepository.orderPay(id, paymentMethod).await()
                defUI.msgEvent.postValue(MessageModel(2))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun buyNow(goodId: Int?, liveId: Int?, quantity: Int?) {
        rxHttpRequest {
            onRequest = {
                settlementDto.value = if (liveId != 0)
                    CartRepository.buyNow(goodId, liveId, quantity).await()
                else
                    CartRepository.buyNow(goodId, quantity).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun buyByGroup(goodId: Int?, quantity: Int?) {
        rxHttpRequest {
            onRequest = {
                settlementDto.value = CartRepository.buyByGroup(goodId, quantity).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun joinBidding(biddingId: Int?, addressId: Int?, paymentMethod: String) {
        rxHttpRequest {
            onRequest = {
                BiddingRepository.toBiddingjoin(biddingId, addressId, paymentMethod).await()
                defUI.msgEvent.postValue(MessageModel(3))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun calculateAmount(params: HashMap<String, Any?>) {
        rxHttpRequest {
            onRequest = {
                settlementDto.value = CartRepository.calculateAmount(params).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
